package cycling.taxi.com.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import cycling.taxi.com.IConstants;
import cycling.taxi.com.R;
import cycling.taxi.com.person.ExitApplication;
import cycling.taxi.com.request.Config;
import cycling.taxi.com.request.RM;
import cycling.taxi.com.request.RequestBean;
import cycling.taxi.com.request.RequestCallback;
import cycling.taxi.com.request.RequestParams;
import cycling.taxi.com.request.RequestUtil;
import cycling.taxi.com.utils.MD5Util;
import cycling.taxi.com.utils.SPUtil;
import cycling.taxi.com.utils.ToastUtil;
import cycling.taxi.com.utils.Urls;

/**
 * Created by Administrator on 2017/5/5.
 */
public class SplashAty extends AppCompatActivity implements RequestCallback{

    private ImageView mImag;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_splash);
        ExitApplication.getInstance().addActivity(this);

        SendDelaySkip();

    }

    private void SendDelaySkip() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String userName = SPUtil.getInstance().getData("userName");
                String password = SPUtil.getInstance().getData("password");
                String token = SPUtil.getInstance().getToken();


                RequestParams params = new RequestParams.Builder()
                        .putParam("mobile", userName)
                        .putParam("password", MD5Util.MD5Encode(password))
                        .putParam("token",token)
                        .build();

                //开始请求发送数据到服务器服务器
                RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
                String url = Urls.getRegister();//回去api路径

                RequestUtil.requestData(requestQueue,
                        IConstants.LOGIN,
                        url,
                        RM.GET,
                        RequestBean.class,
                        params,
                        SplashAty.this);
            }
        },3000);

    }


    @Override
    public void requestSuccess(RequestBean bean, Config config) {
        Intent intent = new Intent(this, HomeAty.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void requestFail(RequestBean bean, Config config) {
        ToastUtil.getInstance().toast("网络异常");
        Intent intent = new Intent(this, LoginAty.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {
        ToastUtil.getInstance().toast("账号密码已过期，请重新登录");
        Intent intent = new Intent(this, LoginAty.class);
        startActivity(intent);
        finish();
    }
}
